package com.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crm.mapper.BusinessMapper;
import com.crm.pojo.common.Result;
import com.crm.pojo.domain.Business;
import com.crm.pojo.domain.BusinessRecord;
import com.crm.pojo.vo.BusinessRecordVo;
import com.crm.service.BusinessRecordService;
import com.crm.service.DictService;
import com.crm.pojo.vo.DictVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/record")
public class BusinessRecordController {


    private final BusinessRecordService businessRecordService;
    private final DictService dictService;
    private final BusinessMapper businessMapper;
    /**
     * 保存沟通记录
     * @param businessRecord 沟通记录信息
     * @return 操作结果
     */
    @PostMapping
    @Transactional
    public Result<Void> saveBusinessRecord(@RequestBody BusinessRecord businessRecord) {

        try {
            // 保存到数据库
            boolean saved = businessRecordService.save(businessRecord);
            businessMapper.genjin(businessRecord.getBusinessId());

            //businessMapper. //给该商机设置为已跟进
            
            if (saved) {
                return Result.success();
            } else {
                return Result.error("保存沟通记录失败");
            }
        } catch (Exception e) {
            log.error("保存沟通记录异常", e);
            return Result.error("保存沟通记录异常: " + e.getMessage());
        }
    }

    /**
     * 根据商机ID查询沟通记录列表
     * @param businessId 商机ID
     * @return 沟通记录列表
     */
    @GetMapping("/list")
    public Result<List<BusinessRecordVo>> list(Integer businessId) {
        if (businessId == null) {
            return Result.error("商机ID不能为空");
        }

        // 构建查询条件
        LambdaQueryWrapper<BusinessRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessRecord::getBusinessId, businessId);
        queryWrapper.orderByDesc(BusinessRecord::getCreateTime);

        List<BusinessRecord> records = businessRecordService.list(queryWrapper);
        
        // 获取数据字典中communication_point类型的数据
        List<DictVo> dictList = dictService.dict("communication_point");
        
        // 转换为VO对象
        List<BusinessRecordVo> voList = new ArrayList<>();
        for (BusinessRecord record : records) {
            BusinessRecordVo vo = new BusinessRecordVo();
            BeanUtils.copyProperties(record, vo);
            
            // 设置关键事项标签数组
            if (record.getKeyItems() != null && !record.getKeyItems().isEmpty()) {
                String[] keyItemIds = record.getKeyItems().split(",");
                List<String> keyLabels = new ArrayList<>();
                
                for (String keyItemId : keyItemIds) {
                    for (DictVo dict : dictList) {
                        if (keyItemId.equals(dict.getDictValue())) {
                            keyLabels.add(dict.getDictLabel());
                            break;
                        }
                    }
                }
                
                vo.setKeys(keyLabels.toArray(new String[0]));
            }
            
            voList.add(vo);
        }
        
        return Result.success(voList);
    }
}